﻿using System;

namespace Gmantis.Controls.Chart.Extended
{
    public class ParametricFunctionSeries : FunctionSeries
    {
        // Fields
        private string _xcodeError = "";
        private Func<double, double> _xfunc;
        private string _xfuncJS;
        private bool _xvalidCode = true;
        private string _ycodeError = "";
        private Func<double, double> _yfunc;
        private string _yfuncJS;
        private bool _yvalidCode = true;

        // Methods
        public override void UpdateData()
        {
            int sampleCount = base.SampleCount;
            double[] numArray = new double[sampleCount];
            double[] numArray2 = new double[sampleCount];
            double num2 = (base.Max - base.Min) / ((double)(sampleCount - 1));
            for (int i = 0; i < sampleCount; i++)
            {
                double parameter = (i == (sampleCount - 1)) ? base.Max : (base.Min + (num2 * i));
                if (this._xvalidCode)
                {
                    string errmsg = "";
                    numArray[i] = base.CalculateValue(this._xfunc, this._xfuncJS, parameter, out errmsg);
                    this.XFunctionCodeError = errmsg;
                    if (!string.IsNullOrEmpty(errmsg))
                    {
                        this._xvalidCode = false;
                    }
                }
                else
                {
                    numArray[i] = double.NaN;
                }
                if (this._yvalidCode)
                {
                    string str2 = "";
                    numArray2[i] = base.CalculateValue(this._yfunc, this._yfuncJS, parameter, out str2);
                    this.YFunctionCodeError = str2;
                    if (!string.IsNullOrEmpty(str2))
                    {
                        this._yvalidCode = false;
                    }
                }
                else
                {
                    numArray2[i] = double.NaN;
                }
            }
            base.ValuesSource = numArray2;
            base.XValuesSource = numArray;
        }

        // Properties
        public Func<double, double> XFunction
        {
            get
            {
                return this._xfunc;
            }
            set
            {
                if (this._xfunc != value)
                {
                    this._xfunc = value;
                    this._xvalidCode = true;
                    this.UpdateData();
                }
            }
        }

        public string XFunctionCode
        {
            get
            {
                return this._xfuncJS;
            }
            set
            {
                if (this._xfuncJS != value)
                {
                    this._xfuncJS = value;
                    this._xvalidCode = true;
                    this.UpdateData();
                }
            }
        }

        public string XFunctionCodeError
        {
            get
            {
                return this._xcodeError;
            }
            private set
            {
                if (this._xcodeError != value)
                {
                    this._xcodeError = value;
                }
            }
        }

        public Func<double, double> YFunction
        {
            get
            {
                return this._yfunc;
            }
            set
            {
                if (this._yfunc != value)
                {
                    this._yfunc = value;
                    this._yvalidCode = true;
                    this.UpdateData();
                }
            }
        }

        public string YFunctionCode
        {
            get
            {
                return this._yfuncJS;
            }
            set
            {
                if (this._yfuncJS != value)
                {
                    this._yfuncJS = value;
                    this._yvalidCode = true;
                    this.UpdateData();
                }
            }
        }

        public string YFunctionCodeError
        {
            get
            {
                return this._ycodeError;
            }
            private set
            {
                if (this._ycodeError != value)
                {
                    this._ycodeError = value;
                }
            }
        }
    }
}
